package com.ebelter.btcomlib.models.bluetooth.products.bloodoxgen;

import android.bluetooth.BluetoothAdapter;

import android.content.Context;
import android.text.TextUtils;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;

import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;

import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;
import com.ebelter.btcomlib.models.bluetooth.products.bloodoxgen.interfaces.BOMesureResult;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 血氧仪
 */

public class BOManager extends BaseManager{

    private static final String TAG = "BOManager";


    private BOBytesAnalysis mBOBytesAnalysis;
    private BOBytesMake mBOBytesMake;




    public BOManager(Context mContext) {
        super(mContext, ProductStyle.BLOODOXYGEN, BOConstant.BO_SERVICE_UUID, new String[]{BOConstant.BO_WRITE_UUID},
                new String[]{BOConstant.BO_READ_UUID1,BOConstant.BO_READ_UUID2,BOConstant.BO_READ_UUID3,BOConstant.BO_READ_UUID4},
                true,500);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBOBytesAnalysis = new BOBytesAnalysis();
        mBOBytesMake = new BOBytesMake();

        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(byte[] data) {
                LogUtils.i(TAG, "BOManager--IDataReadWriteCallback-onCharacteristicChanged--读到-" + BytesUtils.bytes2HexStr(data));
                mBOBytesAnalysis.resultAnalysis(data);
            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });
    }







    public boolean isBondDevice() {
        String add = getBondDeviceAddress();
        return !TextUtils.isEmpty(add);
    }

    public String getBondDeviceAddress() {
        return SpUtil.readString(BOConstant.BO_BOND_DEVICE_ADDRESS, null);
    }

    public void setBondDeviceAddress(String address) {
        SpUtil.writeString(BOConstant.BO_BOND_DEVICE_ADDRESS, address);
    }




    public void setBoMeasureCallback(BOMesureResult bOMesureResult) {
        mBOBytesAnalysis.setBoMeasureCallback(bOMesureResult);
    }




    public void starSendCmd(){
        addSendMsg(mBOBytesMake.getStartSendMessage());
    }



    //获取血氧的等级
    public static int getBOLevel(int score){
        if (score<90){
            return 0;
        }else if (score>=90&&score<=94){
            return 1;
        }else {
            return 2;
        }
    }
    //获取血氧的等级
    public static String getBOLevelDesc(int score){
        if (score<90){
            return getString_(R.string.zhdpd);
        }else if (score>=90&&score<=94){
            return getString_(R.string.qdpd);
        }else {
            return getString_(R.string.zhch);
        }
    }






}
